package com.binfenhulian.chinaenergy.ces.crawler.dispatcher;

import com.binfenhulian.chinaenergy.ces.crawler.core.db.po.auto.CrawlerConfigPO;
import com.binfenhulian.chinaenergy.ces.crawler.pipeline.DefPipeline;
import com.binfenhulian.chinaenergy.ces.crawler.processor.seedlinked.SeedLinkedFromCombProcessor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Spider;

import javax.annotation.Resource;

/**
 * Created by wanghjbuf on 2018/3/13.
 */
@Slf4j
@Service("com.binfenhulian.chinaenergy.ces.crawler.dispatcher.TaskExecutor")
public class TaskExecutor {

    @Resource(name = "com.binfenhulian.chinaenergy.ces.crawler.processor.seedlinked.SeedLinkedFromCombProcessor")
    SeedLinkedFromCombProcessor seedLinkedFromCombProcessor;
    @Resource(name = "com.binfenhulian.chinaenergy.ces.crawler.store.DefPipeline")
    DefPipeline defPipeline;

    /**
     * 执行爬取过程
     * @param crawlerConfigPO
     */
    public void doExecute(CrawlerConfigPO crawlerConfigPO) {

        String crawlerNo = crawlerConfigPO.getNo();
        String edenUrl = crawlerConfigPO.getEdenUrl();
        String nextRegex = crawlerConfigPO.getNextRegex();
        String destContentXpath = crawlerConfigPO.getDestContentXpath();
        String destUrlXpath = crawlerConfigPO.getDestUrlXpath();
        log.info("此次执行参数<>crawlerNo:{},edeUrl:{},nextRegex:{},destContentXpath:{},destUrlXpath:{};", crawlerNo, edenUrl, nextRegex, destContentXpath, destUrlXpath);

        try{
            seedLinkedFromCombProcessor.initPageProcessor(crawlerNo, nextRegex, destContentXpath, destUrlXpath);
            Spider.create(seedLinkedFromCombProcessor).addPipeline(defPipeline).addUrl(edenUrl).thread(10).run();
        }catch (Exception exception){
            log.error("爬虫爬取过程发生错误,失败原因如下!", exception);
        }
    }
}
